﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UDP_Library
{
    /// <summary>
    /// 各种数据的转化，如string、byte[]
    /// </summary>
    public class DataConvert
    {
        public static byte[] string2bytes_Default(string data)
        {
            try
            {
                return Encoding.Default.GetBytes(data);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static string bytes2string_Default(byte[] data)
        {
            try
            {
                string buildStr = "";
                for (int i = 0; i < data.Length; i++)
                {
                    var temp = $"0x{Convert.ToString(Convert.ToInt32(data[i]), 16).ToUpper()}";
                    if (buildStr == "")
                    {
                        buildStr = temp;
                    }
                    else
                    {
                        buildStr = $"{buildStr} {temp}";
                    }
                }


                return buildStr;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 将字符串转化为Byte二进制数组
        /// </summary>
        /// <param name="data">源字符串数据</param>
        /// <param name="bytes">转化的字符串长度</param>
        /// <returns></returns>
        public static byte[] string2bytes_UTF8(string data)
        {
            try
            {
                return Encoding.GetEncoding("utf-8").GetBytes(data);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 将Byte二进制数组转化为字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string bytes2string_UTF8(byte[] data)
        {
            try
            {
                return Encoding.GetEncoding("utf-8").GetString(data);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 将Byte数组（以字节为单位）转化为字符串
        /// </summary>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <param name="ByteSeparate">是否在以字节为单位分隔</param>
        /// <returns></returns>
        public static string byte2string_Hex(byte[] data, int index, int length, bool ByteSeparate)
        {
            if (data == null || data.Length < index + length || index < 0 || length < 1)
            {
                return null;
            }
            if (ByteSeparate)
            {
                return (System.BitConverter.ToString(data, index, length)).Replace("-", " ");
            }
            else
            {
                return (System.BitConverter.ToString(data, index, length)).Replace("-", "");
            }
        }

        /// <summary>
        /// 将字符串(ASCII)转化为Byte二进制数组
        /// </summary>
        /// <param name="data">源字符串数据</param>
        /// <param name="bytes">转化的字符串长度</param>
        /// <returns></returns>
        public static byte[] string2bytes_ASCII(string data)
        {
            try
            {
                return Encoding.ASCII.GetBytes(data);
            }
            catch (Exception ee)
            {
                return null;
            }
        }
        /// <summary>
        /// 将Byte二进制数组转化为字符串(ASCII)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string bytes2string_ASCII(byte[] data)
        {
            try
            {
                return Encoding.ASCII.GetString(data);
            }
            catch (Exception ee)
            {
                return null;
            }
        }
        /// <summary>
        /// 将Byte二进制数组转化为字符串(ASCII)
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string bytes2string_ASCII(byte[] data, int index, int length)
        {
            if (data == null || data.Length < index + length || index < 0 || length < 1)
            {
                return null;
            }
            try
            {
                return Encoding.ASCII.GetString(data, index, length);
            }
            catch (Exception ee)
            {
                return null;
            }
        }

        
       


        
        
    }
    
}
